package com.yc.cloud.openai.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yc.cloud.basic.exception.Asserts;
import com.yc.cloud.openai.dto.request.aigc.ImageGenerateParam;
import com.yc.cloud.openai.dto.request.aigc.ImageMJProcessParam;
import com.yc.cloud.openai.dto.resp.MjImageProcessResp;
import com.yc.cloud.openai.enums.ImageMJProcessEnums;
import com.yc.cloud.openai.service.ImageAiGCService;
import com.yc.cloud.openai.vo.ImageVo;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class MjServiceImpl implements ImageAiGCService {

    String url = "https://api.zhishuyun.com/midjourney/imagine?token=46d1172f4d4a400983af7e9589bbad65";


    @Override
    public String generate(String prompt, String webhookUrl) {
        JSONObject data = new JSONObject();
        data.putOpt("prompt", prompt);
        if (webhookUrl != null && !webhookUrl.isEmpty()) {
            data.putOpt("callback_url", webhookUrl);
        }
        HttpRequest request = HttpUtil.createPost(url)
                .body(JSONUtil.toJsonStr(data))
                .contentType("application/json");
        try (HttpResponse response = request.execute()) {
            return response.body();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return null;
    }

    @Override
    public ImageVo process(ImageMJProcessParam param) {
        JSONObject data = new JSONObject();
        data.putOpt("image_id", param.getImageId());
        //缩放功能
        if (ImageMJProcessEnums.SCALE.getCode().equals(param.getAction())) {
            data.putOpt("action", "upsample" + param.getSequenceNumber());
        }

        HttpRequest request = HttpUtil.createPost(url)
                .body(JSONUtil.toJsonStr(data))
                .contentType("application/json");
        try (HttpResponse response = request.execute()) {
            val result = response.body();
            if (StrUtil.isNotEmpty(result)) {
                val mjImageProcessResp = JSONUtil.toBean(result, MjImageProcessResp.class);
                if (!mjImageProcessResp.getSuccess()) {
                    Asserts.fail("图片处理失败:" + mjImageProcessResp.getDetail());
                }
                val imageVo = new ImageVo();
                imageVo.setRawUrl(mjImageProcessResp.getRawImageUrl());
                imageVo.setCoverUrl(mjImageProcessResp.getImageUrl());
                imageVo.setWidth(mjImageProcessResp.getRawImageWidth());
                imageVo.setHeight(mjImageProcessResp.getRawImageHeight());
                return imageVo;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return null;
    }

    @Override
    public List<ImageVo> generate(ImageGenerateParam param) {
        return new ArrayList<>();
    }


    public static void main(String[] args) {


    }
}
